package com.mkj.draw;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("all")
/**
 * @Author Arrebol
 * @Date 2024/8/4 16:51
 * @Project graphical programming
 * @Description：
 * 弹球小游戏
 */
public class PinBallGame {

    //窗口对象
    private Frame frame = new Frame("弹球小游戏");
    //桌面宽度与高度
    private final int TABLE_WIDTH = 300;
    private final int TABLE_HEIGHT = 400;
    //球拍宽度与高度
    private final int RACKET_WIDTH = 60;
    private final int RACKET_HEIGHT = 20;
    //小球的大小
    private final int BALL_SIZE = 16;
    //定义记录小球的位置坐标
    private int ballX = 120;
    private int ballY = 20;
    //定义变量记录小球在X方向和Y方向移动的速度
    private int speedX = 5;
    private int speedY = 10;
    //定义变量记录球拍的坐标
    private int racketX = 120;
    private final int RACKET_Y = 340;
    //定义变量标识当前游戏是否结束
    private boolean isOver = false;
    //声明一个定时器
    private Timer timer;
    //创建菜单栏
    MenuBar menuBar=new MenuBar();
    Menu menu=new Menu("联系作者");

    //自定义一个类，继承Canvas充当画布
    private class MyCanvas extends Canvas {
        @Override
        public void paint(Graphics g) {
            if (isOver) {
                //游戏结束内容
                g.setColor(Color.BLUE);
                g.setFont(new Font("Times", Font.BOLD, 30));
                g.drawString("真tm菜！", 100, 200);
            } else {
                //游戏中内容
                //绘制小球
                g.setColor(Color.RED);
                g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
                //绘制球拍
                g.setColor(Color.PINK);
                g.fillRect(racketX, RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT);
            }
        }
    }

    //创建会话区域
    private MyCanvas drawArea = new MyCanvas();


    public static void main(String[] args) {
        new PinBallGame().init();
    }

    public void init() {
        //组装视图和游戏逻辑控制
        //球拍坐标变化
        KeyListener keyListener = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获取当前按下的键
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_LEFT) {
                    //按下左箭头，应该向左移动
                    if (racketX > 0) {
                        racketX -= 10;
                    }
                } else if (keyCode == KeyEvent.VK_RIGHT) {
                    //按下右箭头，应该向右移动
                    if (racketX < (TABLE_WIDTH - RACKET_WIDTH)) {
                        racketX += 10;
                    }
                }
            }
        };
        //将监听器与事件源绑定
        frame.addKeyListener(keyListener);
        drawArea.addKeyListener(keyListener);

        //小球坐标的控制
        ActionListener task = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //根据边界范围修正速度
                if (ballX <= 0 || ballX >= (TABLE_WIDTH - BALL_SIZE)) {
                    speedX = -speedX;
                }
                if (ballY <= 0 || (ballY > RACKET_Y - BALL_SIZE && ballX > racketX && ballX < racketX + RACKET_WIDTH)) {
                    speedY = -speedY;
                }
                if(ballY > (RACKET_Y-BALL_SIZE) && (ballX < racketX || ballX > racketX + RACKET_WIDTH)){
                    //当前小球超出球拍范围，游戏结束
                    //停止定时器
                    timer.stop();
                    //修改游戏结束标记
                    isOver=true;
                    //界面重绘
                    drawArea.repaint();
                }
                //更新小球的坐标，重绘界面
                ballX += speedX;
                ballY += speedY;
                drawArea.repaint();
            }
        };
        timer = new Timer(50, task);
        timer.start();
        drawArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
        frame.add(drawArea);
        menuBar.add(menu);
        frame.setMenuBar(menuBar);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}
